package com.beaven.beavenime.view.keyboard

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet

class EnglishKeyboard @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    Keyboard(context, attrs, defStyleAttr) {

    private val keyboardList = KeyboardLayoutSet(context).buildLayoutSet()
    private val bgPaint = Paint().also { paint -> paint.color = Color.BLACK }
    private val keyPaint = Paint().also { paint ->
        paint.color = Color.WHITE
        paint.style = Paint.Style.FILL
        paint.strokeWidth = 5f
        paint.textSize = 25f
        paint.textAlign = Paint.Align.CENTER
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        keyboardList.iterator().forEach { keyCode ->
            val bgRectf = RectF(
                keyCode.paintX,
                keyCode.paintY,
                keyCode.paintX + keyCode.width,
                keyCode.paintY + keyCode.height
            )
            canvas?.drawRoundRect(bgRectf, 5f, 5f, bgPaint)
            val fontMetrics = keyPaint.fontMetrics
            val distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom
            canvas?.drawText(
                keyCode.value.toChar().toString(),
                bgRectf.centerX(),
                bgRectf.centerY() + distance,
                keyPaint
            )
        }
    }
}